﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Sales;
using ESOL.Business.Fin;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.Sales;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Finance
{
    public partial class frmManageARRequest : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private FinReceivedDetailEntity mSelectedReceivedDetail;
        #endregion

        #region Constructor
        public frmManageARRequest()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbOrderType, "CFT", true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbReceivedType, "PAYTYPE", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colCURRENCY, "CS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "STATUS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colORDER_TYPE, "CFT", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colRECEIVED_TYPE, "PAYTYPE", true);


        }
        private string GetCondition()
        {
            string condition = "";
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                MstCustomerEntity customer = (MstCustomerEntity)txtCustomer.Tag;
                condition += " AND RD.CUSTOMER_ID = '" + customer.CustomerId + "' ";
            }
            if (txtReceivedPerson.Tag != null && txtReceivedPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtReceivedPerson.Tag;
                condition += " AND RD.RECEIVED_PERSON = '" + entity.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(cmbOrderType.Text) == false && cmbOrderType.SelectedValue != null)
            {
                condition += " AND RD.ORDER_TYPE = '" + cmbOrderType.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(cmbReceivedType.Text) == false && cmbReceivedType.SelectedValue != null)
            {
                condition += " AND RD.RECEIVED_TYPE = '" + cmbReceivedType.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND RD.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            else
            {
                condition += " AND RD.STATUS != 'STATUS05'";
            }
            if (dtpReceivedDate.EditValue!=null)
            {
                condition += " AND RD.RECEIVED_DATE >= CONVERT(DATETIME,'" + dtpReceivedDate.EditValue.ToString() + "')";
            }
            if (dtpReceivedDateTo.EditValue!=null)
            {
                condition += " AND RD.RECEIVED_DATE <= CONVERT(DATETIME,'" + dtpReceivedDateTo.EditValue.ToString() + "')";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "RECEIVED_DATE DESC";
            DataTable dt = FinReceivedDetailBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        private void ResetButtonsEnable()
        {
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
            btnSubmit.Enabled = false;
            btnApprove.Enabled = false;
            btnPrint.Enabled = false;
            btnInvalid.Enabled = false;
            if (mSelectedReceivedDetail == null)
            {
                return;
            }
            if (mSelectedReceivedDetail.Status == NormalStatus.STATUS02.ToString()
                || mSelectedReceivedDetail.Status == NormalStatus.STATUS04.ToString())
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
            else if (mSelectedReceivedDetail.Status == NormalStatus.STATUS03.ToString())
            {
                btnPrint.Enabled = true;
                btnInvalid.Enabled = true;
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComboBox();
            BindDataGrid();
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.FocusedRowHandle);
            mSelectedReceivedDetail = new FinReceivedDetailEntity(dr);
            ResetButtonsEnable();
        }

        protected override void Click_View()
        {
            if (mSelectedReceivedDetail == null)
            {
                MessageBox.Show("请选择一个到账明细单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainARRequest frm = new frmMaintainARRequest();
            frm.ThisActionType = ActionType.View;
            frm.SelectedReceivedDetail = mSelectedReceivedDetail;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedReceivedDetail == null)
            {
                MessageBox.Show("请选择一个到账明细单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中到账明细单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedReceivedDetail.Status = NormalStatus.STATUS05.ToString();
                mSelectedReceivedDetail.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedReceivedDetail.LastModifiedDate = DateTime.Now;
                bool successful = FinReceivedDetailBs.UpdateFinReceivedDetail(mSelectedReceivedDetail);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedReceivedDetail == null)
            {
                MessageBox.Show("请选择一个到账明细单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainARRequest frm = new frmMaintainARRequest();
            frm.ThisActionType = ActionType.Approve;
            frm.SelectedReceivedDetail = mSelectedReceivedDetail;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedReceivedDetail == null)
            {
                MessageBox.Show("请选择一个到账明细单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainARRequest frm = new frmMaintainARRequest();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedReceivedDetail = mSelectedReceivedDetail;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainARRequest frm = new frmMaintainARRequest();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Print()
        {
            //if (mSelectedReceivedDetail == null)
            //{
            //    MessageBox.Show("请选择一个到账明细单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            //List<FinReceivedDetailEntity> list = new List<FinReceivedDetailEntity>();
            //list.Add(mSelectedReceivedDetail);
            //frmFindPrinter frmPrinter = new frmFindPrinter();
            //if (frmPrinter.ShowDialog() == DialogResult.OK)
            //{
            //    frmPrintReceivedDetail frm = new frmPrintReceivedDetail(list);
            //    frm.SelectedPrinterName = frmPrinter.SelectedPrinterName;
            //    frm.Print();
            //}
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void txtCustomer_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainCustomerQuery frm = new frmMaintainCustomerQuery();
            frm.ShowAllCustomer = true;
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedCustomer != null)
            {
                txtCustomer.Text = frm.SelectedCustomer.CustomerName;
                txtCustomer.Tag = frm.SelectedCustomer;
            }
            else
            {
                txtCustomer.Text = "";
                txtCustomer.Tag = null;
            }
        }

        private void txtReceivedPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtReceivedPerson.Text = frm.SelectedPerson.NameCn;
                txtReceivedPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtReceivedPerson.Text = "";
                txtReceivedPerson.Tag = null;
            }
        }

        private void btnInvalid_Click(object sender, EventArgs e)
        {
            if (mSelectedReceivedDetail == null)
            {
                MessageBox.Show("请选择一个收款记录！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (mSelectedReceivedDetail.Status != "STATUS03")
            {
                MessageBox.Show("请选择一个已审的收款记录！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否确定作废？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedReceivedDetail.Status = "STATUS99";
                mSelectedReceivedDetail.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedReceivedDetail.LastModifiedDate = DateTime.Now;
                bool successful = FinReceivedDetailBs.InvalidReceivedDetail(mSelectedReceivedDetail);
                if (successful)
                {
                    MessageBox.Show("作废成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion
    }
}
